package com.future.lottie.activity;

import android.util.Log;
import android.view.LayoutInflater;
import android.widget.Toast;

import com.future.lottie.base.BaseActivity;
import com.future.lottie.databinding.ActivityProxyBinding;
import com.future.lottie.hell.ITalker;
import com.future.lottie.hell.Talker;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class Proxyctivity extends BaseActivity<ActivityProxyBinding> {


    private ITalker mTalker;

    @Override
    protected ActivityProxyBinding createViewBinding(LayoutInflater inflater) {
        return ActivityProxyBinding.inflate(inflater);
    }



    @Override
    protected void initView() {

        mTalker = new Talker();
        final ITalker t = mTalker;
        Object proxyInstance = Proxy.newProxyInstance(getClassLoader(), new Class[]{ITalker.class}, new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                Toast.makeText(Proxyctivity.this, ""+args[0], Toast.LENGTH_SHORT).show();
                return method.invoke(t, args);
            }
        });
        Class<? extends Proxyctivity> aClass = getClass();
        try {
            Field talker = aClass.getDeclaredField("mTalker");
            talker.setAccessible(true);
            talker.set(this, proxyInstance);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        mViewBinding.btnSay.setOnClickListener(v -> mTalker.sayHello("xuchuanting"));

    }

    @Override
    protected void initData() {

    }
}